﻿TRANGTHAIBILL = function () {
}
TRANGTHAIBILL.columns = new Array("Mã Trạng Thái", "Tên Trạng Thái");
TRANGTHAIBILL.fieldText = new Array("MaTrangThai", "TenTrangThai");
var numberPage = 1;
TRANGTHAIBILL.isInsert = 0;
TRANGTHAIBILL.MaTrangThai = -1;
TRANGTHAIBILL.arrays = new Array();
TRANGTHAIBILL.Init = function () {
    TRANGTHAIBILL.arrays = new Array();
    var obj;
    obj = new CheckField("TenTrangThai", 1, "ErrTenTrangThai");
    TRANGTHAIBILL.Add(obj);
}

TRANGTHAIBILL.Add = function (obj) {
    if (TRANGTHAIBILL.arrays.contains(obj) == false) {
        TRANGTHAIBILL.arrays.push(obj);
    }
}

TRANGTHAIBILL.Insert = function () {
    var serviceUrl = Constants.service + "insertTblTrangThaiBills";
    if (Constants.OnSubmit(TRANGTHAIBILL.arrays) == false) {
        return ;
    }
    var data = {
        MaTrangThai: $("#MaTrangThai").val(),
        TenTrangThai: $("#TenTrangThai").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            TRANGTHAIBILL.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TRANGTHAIBILL.Delete = function (id) {    
    var data = {
        MaTrangThai: id
    };
    var json = JSON.stringify({ item: data });
    a = confirm(Constants.messDelete);
    if (a == false) {
        return;
    }
    var serviceUrl = Constants.service + "deleteTblTrangThaiBills";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (data) {
            TRANGTHAIBILL.GetData();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TRANGTHAIBILL.UpdateItem = function () {
    var serviceUrl = Constants.service + "updateTblTrangThaiBills";
    if (Constants.OnSubmit(TRANGTHAIBILL.arrays) == false) {
        return;
    }
    var data = {
        MaTrangThai: $("#MaTrangThai").val(),
        TenTrangThai: $("#TenTrangThai").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            TRANGTHAIBILL.GetData();            
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TRANGTHAIBILL.CreateTable = function (r, nbpage, data) {
    var myTable = TRANGTHAIBILL.TitleColumns(TRANGTHAIBILL.columns);
    myTable += TRANGTHAIBILL.DataColumnsOftr(data, r, nbpage);
    myTable += "</table>";
    $("#div_list").children().remove();
    $("#div_list").append(myTable);
    $("#div_content").show();
}

TRANGTHAIBILL.GetData = function () {
    TRANGTHAIBILL.MaTrangThai = -1;
    var serviceUrl = Constants.service + "getAllTblTrangThaiBills";
    var data = { url: window.location.host };
    var json = JSON.stringify({ item: data });
    TRANGTHAIBILL.isInsert = 0;
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var results = msg.getAllTblTrangThaiBillsResult;
            var dataTables = results;
            TRANGTHAIBILL.CreateTable(Constants.rows, numberPage, dataTables);
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TRANGTHAIBILL.TitleColumns = function (_columns) {
    var myColumns = "<table class=\"rounded-corner\">";
    myColumns += "<thead><tr>";
    for (var i = 0; i < _columns.length; i++) {
        myColumns += "<th class=\"rounded\" scope=\"col\">" + _columns[i] + "</th>";
    }
    myColumns += "<th class=\"rounded\" scope=\"col\">Edit</th>";
    myColumns += "<th class=\"rounded-q4\" scope=\"col\">Delete</th>";
    myColumns += "</tr></thead>";
    var n = _columns.length + 1;
    myColumns += "<tfoot><tr><td class=\"rounded-foot-left\" colspan=\"" + n + "\"><em> </em></td>";
    myColumns += "<td class=\"rounded-foot-right\">&nbsp;</td>";
    myColumns += "</tr></tfoot>";
    return myColumns;
}

TRANGTHAIBILL.DataColumnsOftr = function (data, rs, _numberPage) {
    var myDataColumns = "<tbody>";
    var n = data.length;
    var j = (_numberPage - 1) * rs;
    var r = rs;
    console.info("TRANGTHAIBILL.DataColumnsOftr");
    console.dir(data);
    for (var i = j; i < r && i < n; i++) {
        var id = data[i].MaTrangThai;
        myDataColumns += "<tr onclick='return TRANGTHAIBILL.OnClick(" + id + ")'" + " id=\"" + id + "\">";        
        myDataColumns += "<td>" + data[i].MaTrangThai + "</td>"
        myDataColumns += "<td>" + data[i].TenTrangThai + "</td>"        
        myDataColumns += "<td><img onclick='return TRANGTHAIBILL.EditItem("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/user_edit.png\"></td>";
        myDataColumns += "<td><a class=\"ask\" ><img onclick='return TRANGTHAIBILL.Delete("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/trash.png\"></a></td>";
    }
    myDataColumns += "</tbody>";
    return myDataColumns;
}
TRANGTHAIBILL.OnClick = function (id) {
    TRANGTHAIBILL.MaTrangThai = id;
    $(".active").removeClass();
    $("#" + id).addClass("active");
    return false;
}

TRANGTHAIBILL.OnClickEdit = function () {
    if (TRANGTHAIBILL.MaTrangThai == -1) {
        alert(Constants.selectEdit);
    }
    else
        TRANGTHAIBILL.EditItem(TRANGTHAIBILL.MaTrangThai);
}

TRANGTHAIBILL.OnClickDelete = function () {
    if (TRANGTHAIBILL.MaTrangThai == -1) {
        alert(Constants.selectDelete);
    }
    else
        TRANGTHAIBILL.Delete(TRANGTHAIBILL.MaTrangThai);
}

TRANGTHAIBILL.EditItem = function (obj_id) {    
    var temp_item = "Modules/ttbill/Edit";   
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Sửa" });
            TRANGTHAIBILL.GetItem(obj_id);            
        }
    });
}

TRANGTHAIBILL.GetItem = function (id) {

    var serviceUrl = Constants.service + "getItemByIdTblTrangThaiBill";
    var data = { MaTrangThai: id };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getItemByIdTblTrangThaiBillResult;
            if (result != null && result != undefined) {
                $("#MaTrangThai").val(result.MaTrangThai);
                $("#TenTrangThai").val(result.TenTrangThai);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TRANGTHAIBILL.Clear = function () {
    Constants.clearAll(TRANGTHAIBILL.fieldText);
}

TRANGTHAIBILL.AddNewItem = function () {
    var temp_item = "Modules/ttbill/AddNew";
    TRANGTHAIBILL.isInsert = 1;
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Thêm Mới" });
            TRANGTHAIBILL.getMa();
        }
    });
}

TRANGTHAIBILL.getMa = function () {
    var serviceUrl = Constants.service + "getMaTblTrangThaiBill";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        //data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getMaTblTrangThaiBillResult;
            if (result != undefined && result != null) {
                $("#MaTrangThai").val(result);
                
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TRANGTHAIBILL.Enter = function () {
    $("#MaTrangThai").keypress(function (event) {
        if (event.keyCode == 13) {
            $("#TenTrangThai").focus();
            event.preventDefault();
        }
    });
    $("#TenTrangThai").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            if (TRANGTHAIBILL.isInsert == 1) {
                TRANGTHAIBILL.Insert();
            }
            else {
                TRANGTHAIBILL.UpdateItem();
            }
        }
    });
}
$(document).ready(function () {
    TRANGTHAIBILL.GetData();
    TRANGTHAIBILL.Init();
    // TRANGTHAIBILL.Endters();
    $("#search").keypress(function (event) {
        if (event.keyCode == 13) {
            //fnSearch($("#search").val());
            //createTable(rows, numberPage, dataSearch);
            //event.preventDefault();
        }
    });
});